﻿using Meiam.System.Interfaces;
using Meiam.System.Interfaces.IService;
using Meiam.System.Model.Entity;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Meiam.System.Model.Dto;
using Microsoft.Extensions.Configuration;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using SqlSugar;
using System.Text;
using System.Data;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Meiam.System.Model;
using Microsoft.Extensions.Logging;
using System.IO;
using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;
using System.Xml.Linq;
using System.Reflection.Metadata;
using OfficeOpenXml.FormulaParsing.Excel.Functions.RefAndLookup;
using Castle.Core.Resource;
using MailKit.Search;

namespace Meiam.System.Hostd.Controllers.Gokin
{
    /// <summary>
    /// 发运计划_已分配数量回写
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class TMSShipPlanQtyReturnController : ControllerBase
    {
        private readonly ILogger _logger;
        public readonly IWMSTokenCheckService _WMSTokenCheckService;
        public readonly string con;
        public readonly string con_zh;
        public readonly string con_qh;
        public readonly string con_yb;
        public readonly string con_gz;


        public TMSShipPlanQtyReturnController(ILogger<TMSShipPlanQtyReturnDto> logger, IWMSTokenCheckService wmsTokenCheckService, IConfiguration _configuration, ILoggerFactory loggerFactory)
        {
            _WMSTokenCheckService = wmsTokenCheckService;
            con = _configuration.GetConnectionString("DDM_DbConnection_ZH");
            _logger = loggerFactory.CreateLogger<TMSShipPlanQtyReturnController>();
        }

        /// <summary>
        /// 发运计划_已分配数量回写
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public IActionResult PostPlanQtyReturn([FromBody] TMSShipPlanQtyReturnRequestDto strparm)
        {
            string strMsg = "";
            StringBuilder strSql = new StringBuilder();
            List<string> strSqlList = new List<string>();
            Dictionary<string, string> dic = new Dictionary<string, string>(); //查询公用dic
            TMSShipPlanQtyReturnResponseDto re = new TMSShipPlanQtyReturnResponseDto();
            string waybill_no = strparm.waybill_no;
            string in_con = "";//目标WMS数据库
            try
            {
                if (waybill_no == "")
                {
                    strMsg = "waybill_no must be not null";
                }
               

                if (strMsg == "")
                {                  
                    dic = new Dictionary<string, string>(); //查询公用dic
                    dic.Add("waybill_no", waybill_no);
                    DataTable so_dt = MethodHelp.GetTableInfo("TMS0086", dic, con);//运单订单明细获取
                    if (so_dt.Rows.Count > 0)
                    {
                        strSqlList = new List<string>();

                        foreach (DataRow item in so_dt.Rows)
                        {
                            double yfpmount = 0;
                            yfpmount = DB.ToDouble(item["ship_qty"].ToString());

                            dic = new Dictionary<string, string>(); //查询公用dic
                            dic.Add("yfpmount", yfpmount.ToString());
                            dic.Add("ccode", item["plan_no"].ToString());
                            string strSQL = MethodHelp.GetTranSQL("TMS0087", dic, con);
                            if (strSQL != "")
                            {
                                strSqlList.Add(strSQL.ToString());
                            }
                            else
                            {
                                strMsg = "Parameter conversion failed";
                            }
                        }

                        if (strMsg=="")
                        {
                            if (strSqlList.Count > 0)
                            {
                                bool A = DB.ExecuteNoQueryTran(strSqlList, con);
                                if (A)
                                {

                                }
                                else
                                {
                                    strMsg = "Database update failed";
                                }

                            }
                            else
                            {
                                strMsg = "No insertion data present";
                            }
                        }
                        
                    }
                    else
                    {
                        strMsg = "TMS找不到运单=<" + waybill_no + "> 相关信息，请确认后再试！ ";
                    }
                }
            }
            catch (Exception ex)
            {
                strMsg = ex.ToString();
            }

            if (strMsg == "")
            {
                re.code = 0;
                re.message = "";
            }
            else
            {
                re.code = 500;
                re.message = strMsg;

            }


            _logger.LogInformation(JsonConvert.SerializeObject(re));
            return new JsonResult(re);

        }


    }
}
